﻿using Btssio.Deezer;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using RDC.DATA.Class;
using Btssio.Musique;
using RDC.DATA.Class.Météo;

namespace RDC.UI
{
    public partial class frmDemarrage : Form
    {
        #region champs
        private Artist selectedArtist = null;
        private Event selectedConcert = null;
        private const string MESSAGE_SELECTION_ARTISTE = "Sélectionnez d'abord un artiste dans la liste des résultats de la recherche";
        private const string MESSAGE_RECHERCHE_ARTISTE = "Merci de patienter, je recherche les artistes...";
        private const string MESSAGE_ARTISTE_INTROUVABLE = "Artiste introuvable";
        private const string MESSAGE_RECHERCHE_CONCERT = "Merci de patienter, je recherche les concerts disponibles...";
        private const string MESSAGE_CONCERT_INTROUVABLE = "Il n'y a aucun concert dans ce lieu.";

        #endregion

        #region constructeurs
        public frmDemarrage()
        {
            InitializeComponent();
            wbGoogleMap.ScriptErrorsSuppressed = true;
        }

        #endregion

        #region méthodes gestionnaire d'événement

        private void btnRechercher_Click(object sender, EventArgs e)
        {
            lblMessageRecherche.Text = MESSAGE_RECHERCHE_ARTISTE;
            lblMessageRecherche.Refresh();
            grbConcert.Visible = false;
            selectedArtist = null;
            montrerArtistes();
            lblMessageRecherche.Text = string.Empty;
        }

        private void btnRechercherVille_Click(object sender, EventArgs e)
        {
            lblMessageRechercheVille.Text = MESSAGE_RECHERCHE_CONCERT;
            lblMessageRechercheVille.Refresh();
            grbConcertResultat.Visible = true;
            selectedConcert = null;
            montrerConcert();
            lblMessageRechercheVille.Text = string.Empty;
        }
        private void tbpArtiste_Enter(object sender, EventArgs e)
        {
            txtArtisteRecherche.Focus();
        }
        private void btnChoisirFichier_Click(object sender, EventArgs e)
        {
            diaMultimedia.ShowDialog();
        }
        private void photoSelectedArtist_Click(object sender, EventArgs e)
        {
            grbConcert.Visible = true;
            picSpinner.Visible = true;
            selectedArtist = (Artist)((PictureBox)sender).Tag;
            txtArtisteRecherche.Text = selectedArtist.name;
            montrerListeConcert();

        }
        private void photoSelectConcert_Click(object sender, EventArgs e)
        {
            selectedConcert = (Event)((PictureBox)sender).Tag;
            tbc.SelectTab(1);
            remplirTabConcert();
            MontrerLaMétéo();
            wbGoogleMap.Navigate(GoogleMapAPI.GeturlGoogleMap(selectedConcert.uneVenue.uneLocation.latitude, selectedConcert.uneVenue.uneLocation.longitude));
        }

        private void txtArtisteRecherche_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return) btnRechercher_Click(btnRechercher, new EventArgs());

        }

        private void txtRechercheVille_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return) btnRechercherVille_Click(btnRechercherVille, new EventArgs());

        }
        private void lkLblWebSite_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            wbGoogleMap.Navigate(selectedConcert.urlWebsite);
        }

        private void lkLblWebSiteLastFm_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            wbGoogleMap.Navigate(selectedConcert.uneVenue.url);
        }

        #endregion

        #region méthodes
        private void montrerListeConcert() 
        {
            List<Event> lesEvents = LastFMAPI.getEventByArtist(selectedArtist.name);
            if (lesEvents.Count == 0)
            {
                MessageBox.Show("Aucun concert Pour l'artiste " + selectedArtist.name);
            }
            else
            {
                flpConcert.Controls.Clear();
                PictureBox photo = null;
                FlowLayoutPanel cadre = null;
                Label nomConcert = null;
                flpConcert.SuspendLayout();


                foreach (Event unEvent in lesEvents)
                {

                    cadre = new FlowLayoutPanel();
                    cadre.FlowDirection = FlowDirection.TopDown;
                    cadre.BorderStyle = BorderStyle.Fixed3D;
                    cadre.AutoSize = true;
                    nomConcert = new Label();
                    nomConcert.AutoEllipsis = true;
                    nomConcert.Text = unEvent.titre + " " + unEvent.uneVenue.name;
                    nomConcert.Tag = unEvent;
                    photo = new PictureBox();
                    photo.SizeMode = PictureBoxSizeMode.AutoSize;
                    photo.ImageLocation = unEvent.urlAffiche.ToString();
                    photo.Tag = unEvent;
                    photo.Cursor = Cursors.Hand;
                    photo.Click += new EventHandler(photoSelectConcert_Click);

                    cadre.Controls.Add(photo);
                    cadre.Controls.Add(nomConcert);
                    flpConcert.Controls.Add(cadre);
                }
                flpConcert.ResumeLayout();
            }
        }
        private void montrerArtistes()
        {
            flpArtistes.Controls.Clear();
            List<Artist> lesArtistes = DeezerApi.getAllArtistsByName(txtArtisteRecherche.Text);
            if (lesArtistes.Count != 0)
            {
                erp.SetError(txtArtisteRecherche, string.Empty);
                PictureBox photo = null;
                FlowLayoutPanel cadre = null;
                Label nomArtiste = null;
                flpArtistes.SuspendLayout();
                foreach (Artist unArtiste in lesArtistes)
                {

                    cadre = new FlowLayoutPanel();
                    cadre.FlowDirection = FlowDirection.TopDown;
                    cadre.BorderStyle = BorderStyle.Fixed3D;
                    cadre.AutoSize = true;

                    nomArtiste = new Label();
                    nomArtiste.AutoEllipsis = true;
                    nomArtiste.Text = unArtiste.name;
                    photo = new PictureBox();
                    photo.SizeMode = PictureBoxSizeMode.AutoSize;
                    photo.ImageLocation = unArtiste.picture.ToString();
                    photo.Tag = unArtiste;
                    photo.Cursor = Cursors.Hand;
                    photo.Click += new EventHandler(photoSelectedArtist_Click);

                    cadre.Controls.Add(photo);
                    cadre.Controls.Add(nomArtiste);
                    flpArtistes.Controls.Add(cadre);
                }
                flpArtistes.ResumeLayout();
            }
            else erp.SetError(txtArtisteRecherche, MESSAGE_ARTISTE_INTROUVABLE);
        }
        private void montrerConcert()
        {
            flpConcertVille.Controls.Clear();
            List<Event> lesEvents = LastFMAPI.getGeoEvent(txtRechercherVille.Text);
            if (lesEvents.Count != 0)
            {
                erp.SetError(txtRechercherVille, string.Empty);
                PictureBox photo = null;
                FlowLayoutPanel cadre = null;
                Label unConcert = null;
                flpConcertVille.SuspendLayout();


                foreach (Event unEvent in lesEvents)
                {

                    cadre = new FlowLayoutPanel();
                    cadre.FlowDirection = FlowDirection.TopDown;
                    cadre.BorderStyle = BorderStyle.Fixed3D;
                    cadre.AutoSize = true;

                    unConcert = new Label();
                    unConcert.AutoEllipsis = true;
                    unConcert.Text = unEvent.titre;
                    photo = new PictureBox();
                    photo.SizeMode = PictureBoxSizeMode.AutoSize;
                    photo.ImageLocation = unEvent.urlAffiche.ToString();
                    photo.Tag = unEvent;
                    photo.Cursor = Cursors.Hand;
                    photo.Click += new EventHandler(photoSelectConcert_Click);

                    cadre.Controls.Add(photo);
                    cadre.Controls.Add(unConcert);
                    flpConcertVille.Controls.Add(cadre);
                } 

                flpConcertVille.ResumeLayout();
            }
            else{MessageBox.Show("Aucune Ville n'a été trouvé" + selectedArtist.name);}
        }
        private void remplirTabConcert()
        {
            pbConcert.Visible = true;
            lblNomConcert.Visible = true;
            lblWebSiteConcert.Visible = true;
            lblLastFmWebSiteConcert.Visible = true;
            lblVilleConcert.Visible = true;
            lblPaysConcert.Visible = true;
            lblRue.Visible = true;
            lblDate.Visible = true;
            lblTelConcert.Visible = true;
            lkLblWebSite.Visible = true;
            lkLblWebSiteLastFm.Visible = true;

            pbConcert.ImageLocation = selectedConcert.urlAffiche;
            lblNomConcert.Text = "Nom du concert : " + selectedConcert.titre;
            lblWebSiteConcert.Text = "Siteweb Artiste :"; lkLblWebSite.Text = selectedConcert.urlWebsite;
            lblLastFmWebSiteConcert.Text = "Siteweb LastFM : "; lkLblWebSiteLastFm.Text = selectedConcert.uneVenue.url;
            lblVilleConcert.Text = "Ville : " + selectedConcert.uneVenue.uneLocation.city;
            lblPaysConcert.Text = "Pays : " + selectedConcert.uneVenue.uneLocation.country;
            lblRue.Text = "Rue : " + selectedConcert.uneVenue.uneLocation.street;
            lblDate.Text = "Date : " + selectedConcert.startDate;
            lblTelConcert.Text = "Tel : " + selectedConcert.uneVenue.phonenumber;
            
           


        }
        private void MontrerLaMétéo()
        {

           
            DateTime date = DateTime.Now.Date;
            ApiMeteo test = new ApiMeteo(selectedConcert.uneVenue.uneLocation.postalCode, selectedConcert.startDate);
            test.getLaMeteo(selectedConcert.uneVenue.uneLocation.city,selectedConcert.startDate.ToString());
            pbcMeteoTemps.Visible = true;
            pbcMeteoVent.Visible = true;



            if (test.MeteoNuage == "sky is clear")
            {
                if (test.MeteoTemps == "rain")
                {
                    pbcMeteoTemps.Image = global::RDC.UI.Properties.Resources.Pluiesoleil;
                }

                else if (test.MeteoTemps == "snow")
                {
                    pbcMeteoTemps.Image = global::RDC.UI.Properties.Resources.Neige;
                }
                else
                    pbcMeteoTemps.Image = global::RDC.UI.Properties.Resources.soleil;
            }

            else if (test.MeteoNuage.Contains("clouds"))
            {
                if (test.MeteoTemps == "rain")
                {
                    pbcMeteoTemps.Image = global::RDC.UI.Properties.Resources.Pluie;
                }
                else if (test.MeteoTemps == "snow")
                {
                    pbcMeteoTemps.Image = global::RDC.UI.Properties.Resources.Neige;
                }
                else
                    pbcMeteoTemps.Image = global::RDC.UI.Properties.Resources.Nuageux;
            }

            if (test.MeteoVent.Contains("breeze"))
            {
                pbcMeteoVent.Image = global::RDC.UI.Properties.Resources.Vent;
            }
        #endregion

        }
    }
}
